# coding=utf-8
from BlueFoxMath import cos, sin, floor, pi, ceil, MatProduct
from ..ToolsConfig import *


def FixPosition(pos):
    """
    修正坐标, 通常用于方块坐标
    """
    pos = list(pos)
    for index in range(pos.__len__()):
        pos[index] = int(floor(pos[index]) if ceil(pos[index]) - pos[index] > 0.5 else ceil(pos[index]))
    return tuple(pos)


class OriginCoord(object):
    """
    对三维坐标进行原点变换
    """

    def __init__(self, originPos):
        self.originPos = originPos

    def CalculatePos(self, pos):
        """
        计算变换
        """
        x, y, z = pos
        return x - self.originPos[0], y - self.originPos[1], z - self.originPos[2]

    def AbsPos(self, pos):
        """
        逆计算变换
        """
        x, y, z = pos
        return x + self.originPos[0], y + self.originPos[1], z + self.originPos[2]


class RotAxes(object):
    X = 0
    Y = 1
    Z = 2


BlockFaceRot = [90, 0, 270, 180]  # 四面向方块对于不同aux对应不同旋转角度


class RotCoord(object):
    """
    对三维坐标进行旋转变换
    """

    def __init__(self, rot, axes):
        """
        :param rot:旋转角度
        :param axes:旋转轴
        """
        self.rotation = rot / 180.0 * pi
        if axes == RotAxes.X:
            self.rotMat = [
                [1, 0, 0],
                [0, cos(self.rotation), -sin(self.rotation)],
                [0, sin(self.rotation), cos(self.rotation)]
            ]
        elif axes == RotAxes.Y:
            self.rotMat = [
                [cos(self.rotation), 0, sin(self.rotation)],
                [0, 1, 0],
                [-sin(self.rotation), 0, cos(self.rotation)]
            ]
        else:
            self.rotMat = [
                [0, cos(self.rotation), -sin(self.rotation)],
                [0, sin(self.rotation), cos(self.rotation)],
                [0, 0, 1],
            ]

    def CalculatePos(self, pos):
        """
        计算变换
        """
        return MatProduct(pos, self.rotMat)
